function [Q, LL] = l_mog_E_step(X, mog)
% implements the E-step of the algorithm. The parameters are X,
% the data, and MOG the current parameter values. The function does not change the parameters but
% computes Q, a matrix that contains the probability of each mixture component given the data p(z|x, θ),
% and LL, the log-likelihood of the data set under the mixture model.
%
% 2012 Maarten Inja & Chiel Kooijman.

s = size(mog, 2);

Q = zeros(size(X, 1), s);

for i = 1:s
    mv = lmvnpdf(X, mog{i}.MU, mog{i}.SIGMA);
    Q(:, i) = log(mog{i}.PI) + mv;
end


vec = zeros(size(X, 1), 1);
for i = 1:size(X, 1)
    vec(i) = logsumexpvec(Q(i, :));
end

Q -= repmat(vec, 1, s);
Q = exp(Q);
%repmat(vec, 1, size(X, 1));

LL = logsumexpvec(vec');
